package com.woniuxy.three.pms.room.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.woniuxy.three.pms.bean.RoomBean;
import com.woniuxy.three.pms.bean.RoomTypeBean;
import com.woniuxy.three.pms.room.service.IRoomService;
import com.woniuxy.three.pms.roomtype.service.IRoomTypeService;

@Controller
public class RoomController {

	@Autowired
	private IRoomService service;
	private IRoomTypeService rtservice;
	
	@PostMapping("/room")
	@ResponseBody
	public String addRoom(@Validated  @RequestBody RoomBean room,BindingResult result,ModelMap map) {
		System.out.println(room);
		if(result.hasErrors()) {
			List<FieldError> list = result.getFieldErrors();
			for (FieldError fieldError : list) {
				map.put("error_"+fieldError.getField(), fieldError.getDefaultMessage());
			}
			return "false";
		}
		boolean boo = service.addRoom(room);
		if(boo) {
			RoomTypeBean roomt = rtservice.findById(room.getId());
			roomt.setStock1(roomt.getStock1()+1);
			roomt.setStock2(roomt.getStock2()+1);
			roomt.setStock3(roomt.getStock3()+1);
			roomt.setStock4(roomt.getStock4()+1);
			roomt.setStock5(roomt.getStock5()+1);
			roomt.setStock6(roomt.getStock6()+1);
			roomt.setStock7(roomt.getStock7()+1);
			boolean boo2 = rtservice.changeRoomType(roomt);
			if(boo2) {
				return "true";
			}
		}
		return "false";
	}
	
	@GetMapping("/rooms/{roomType}/{roomstatus}/{hotelId}/{pageNo}/{pageSize}")
	@ResponseBody
	public PageInfo<RoomBean> findAll(@PathVariable("roomType")Integer roomType,@PathVariable("roomstatus")Integer RoomStatus, @PathVariable("pageNo")Integer  pageNo,@PathVariable("pageSize")Integer PageSize,@PathVariable("hotelId")Integer hotelId){
		PageInfo<RoomBean> page = service.findByType(roomType, RoomStatus, pageNo, PageSize, hotelId);
		return page;
	}
	
	@GetMapping("/rooms/{pageNo}/{pageSize}")
	@ResponseBody
	public PageInfo<RoomBean> findAll( @PathVariable("pageNo")Integer  pageNo,@PathVariable("pageSize")Integer PageSize){
		PageInfo<RoomBean> page = service.findByType(null, null, pageNo, PageSize, 1);
		return page;
	}
	
	
	@GetMapping("/room/{roomId}")
	@ResponseBody
	public RoomBean findByid(@PathVariable("roomId")Integer roomId) {
		RoomBean room = service.findById(roomId);
		return room;
	}
	
	@GetMapping("/room/{hotelId}/{RoomNumber}")
	@ResponseBody
	public RoomBean findByRoomNumber(@PathVariable("RoomNumber")String RoomNumber,@PathVariable("hotelId")Integer hotelId) {
		RoomBean room = service.findByRoomNumber(RoomNumber, hotelId);
		return room;
	}
	
	@PutMapping("/room")
	@ResponseBody
	public boolean changeRoom(@RequestBody RoomBean room) {
		boolean boo = service.changeRoom(room);
		return boo;
	}
	
	
}
